//code by Heskey0
//https://space.bilibili.com/455965619
//https://github.com/Heskey0

using System;
using System.Collections.Generic;
using Common.DesignPattern;
using Common.Extension;
using UnityEngine;

namespace Teresa
{
    public class UIManager : ManagerBase
    {
        private GameObject _root;    //ui_root
        private Dictionary<UILayer, GameObject> _layerNodeDic = new Dictionary<UILayer, GameObject>();

        public UIManager()
        {
            _root = Resources.Load<GameObject>(AppConfig.Path.UI.UIRootPath);
            GameObject.DontDestroyOnLoad(_root);
            for (int i = 0; i < (int) UILayer.End; i++)
            {
                var nodeLayer = (UILayer) i;
                var node = _root.transform.Find(nodeLayer.ToString()).gameObject;

                _layerNodeDic.Add(nodeLayer, node);
            }
        }
        
        public GameObject OpenPanel(string path, UILayer uiLayer)
        {
            return Resources.Load<GameObject>(path)
                .Instantiate()
                .SetParent(_layerNodeDic[uiLayer]);
        }

        public void ClosePanel(GameObject target)
        {
            GameObject.Destroy(target);
        }

        public void Clear(UILayer uiLayer)
        {
            _layerNodeDic[uiLayer].DestroyAllChildren();
        }
    }

    public enum UILayer
    {
        Bottom,
		Normal,
		Top,
		
        End,
    }
}